package com.yanqu.road.server.manager.activity.apprentice.pb;

import com.yanqu.road.entity.activity.apprentice.*;
import com.yanqu.road.entity.activity.apprentice.data.*;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.ApprenticeProto;
import com.yanqu.road.server.manager.user.CrossUserMgr;

public class ApprenticePb {

    /**
     * build 玩家数据
     * @param userData
     * @return
     */
    public static ApprenticeProto.ApprenticeUserData.Builder buildUserData(ApprenticeUserData userData, int canSelectProjectNum) {
        ApprenticeProto.ApprenticeUserData.Builder builder = ApprenticeProto.ApprenticeUserData.newBuilder();
        builder.setActivityId(userData.getActivityId());
        builder.setUserId(userData.getUserId());
        builder.setLevel(userData.getLevel());
        builder.setProgramNum(userData.getProgramNum());
        builder.setTalent1(userData.getTalent1());
        builder.setTalent2(userData.getTalent2());
        builder.setTalent3(userData.getTalent3());
        builder.setTalent4(userData.getTalent4());
        builder.setTalent5(userData.getTalent5());
        builder.setAllTalent(userData.getTalent1() + userData.getTalent2() + userData.getTalent3() + userData.getTalent4() + userData.getTalent5());
        builder.addAllWaitSelectProjects(userData.getWaitProjects());
        builder.setExamDiffNum(userData.getExamDiffNum());
        builder.setExamData(buildExamData(userData.getExamData()));
        for (ApprenticeBuffProjectData buffProjectData : userData.getBuffProjectIds()) {
            builder.addBuffProject(buildBuffProjectData(buffProjectData));
        }
        builder.setCanSelectProjectNum(canSelectProjectNum);
        builder.setRedData(buildRedData(userData));
        return builder;
    }

    /**
     * build 红点数据
     * @param userData
     * @return
     */
    public static ApprenticeProto.ApprenticeRedData.Builder buildRedData(ApprenticeUserData userData) {
        ApprenticeProto.ApprenticeRedData.Builder builder = ApprenticeProto.ApprenticeRedData.newBuilder();
        builder.setPvpLog(userData.isPvpRedFlag());
        builder.setBanquetRed(userData.isBanquetRedFlag());
        return builder;
    }

    /**
     * build buff数据
     * @param buffProjectData
     * @return
     */
    public static ApprenticeProto.ApprenticeBuffProjectData.Builder buildBuffProjectData(ApprenticeBuffProjectData buffProjectData) {
        ApprenticeProto.ApprenticeBuffProjectData.Builder builder = ApprenticeProto.ApprenticeBuffProjectData.newBuilder();
        builder.setProjectId(buffProjectData.getProjectId());
        builder.setNum(buffProjectData.getNum());
        return builder;
    }

    /**
     * build 小考数据
     * @param examData
     * @return
     */
    public static ApprenticeProto.ApprenticeExamData.Builder buildExamData(ApprenticeUserExamData examData) {
        if (examData == null) {
            return null;
        }
        ApprenticeProto.ApprenticeExamData.Builder builder = ApprenticeProto.ApprenticeExamData.newBuilder();
        builder.setTargetTalent1(examData.getTargetTalent1());
        builder.setTargetTalent2(examData.getTargetTalent2());
        builder.setTargetTalent3(examData.getTargetTalent3());
        builder.setTargetTalent4(examData.getTargetTalent4());
        builder.setTargetTalent5(examData.getTargetTalent5());
        builder.setTargetTalentAll(examData.getTargetTalent1() + examData.getTargetTalent2() + examData.getTargetTalent3() + examData.getTargetTalent4() + examData.getTargetTalent5());
        builder.setTalent1(examData.getTalent1());
        builder.setTalent2(examData.getTalent2());
        builder.setTalent3(examData.getTalent3());
        builder.setTalent4(examData.getTalent4());
        builder.setTalent5(examData.getTalent5());
        builder.setTalentAll(examData.getTalent1() + examData.getTalent2() + examData.getTalent3() + examData.getTalent4() + examData.getTalent5());
        builder.setExamIndex(examData.getExamIndex());
        builder.setLevelConfigId(examData.getLevelConfigId());
        return builder;
    }

    /**
     * build 天赋数据
     * @param data
     * @return
     */
    public static ApprenticeProto.ApprenticeTalentData.Builder buildTalentData(ApprenticeTalentData data) {
        if (data == null) {
            return null;
        }
        ApprenticeProto.ApprenticeTalentData.Builder builder = ApprenticeProto.ApprenticeTalentData.newBuilder();
        builder.setTalentType(data.getType());
        builder.setTalentValue(data.getValue());
        return builder;
    }

    /**
     * build 宴会结果
     * @param data
     * @return
     */
    public static ApprenticeProto.ApprenticeBanquetResult.Builder buildBanquetResult(ApprenticeBanquetResult data) {
        if (data == null) {
            return null;
        }
    	ApprenticeProto.ApprenticeBanquetResult.Builder builder = ApprenticeProto.ApprenticeBanquetResult.newBuilder();
    	builder.setMyAddTalent(buildTalentData(data.getMyAddTalent()));
        builder.setTargetUserName(data.getTargetUserName());
        builder.setTargetUserLevel(data.getTargetUserLevel());
        builder.setTargetAddTalent(buildTalentData(data.getTargetAddTalent()));
        builder.setAddRate(data.getAddRate());
    	return builder;
    }

    /**
     * build pvp结果
     * @param data
     * @return
     */
    public static ApprenticeProto.ApprenticePvpResult.Builder buildPvpResult(ApprenticePvpResult data) {
        if (data == null) {
            return null;
        }
    	ApprenticeProto.ApprenticePvpResult.Builder builder = ApprenticeProto.ApprenticePvpResult.newBuilder();
        for (ApprenticeTalentData talentData : data.getMyTalents()) {
            builder.addMyTalents(buildTalentData(talentData));
        }
        for (ApprenticeTalentData talentData : data.getTargetTalents()) {
            builder.addTargetTalents(buildTalentData(talentData));
        }
        builder.setWinNum(data.getWinNum());
        builder.setAddTalent(buildTalentData(data.getAddTalent()));
        builder.setTargetUserName(data.getTargetUserName());
        builder.setTargetUserLevel(data.getTargetUserLevel());
        builder.setAddRate(data.getAddRate());
        return builder;
    }

    /**
     * build 单个项目执行结果
     * @param data
     * @return
     */
    public static ApprenticeProto.ApprenticeProjectResult.Builder buildProjectResult(ApprenticeDoOneProjectResult data) {
        ApprenticeProto.ApprenticeProjectResult.Builder builder = ApprenticeProto.ApprenticeProjectResult.newBuilder();
        builder.setProjectId(data.getProjectId());
        builder.setTalentData(buildTalentData(data.getTalentData()));
        if (data.getBanquetResult() != null) {
            builder.setBanquetResult(buildBanquetResult(data.getBanquetResult()));
        }
        if (data.getPvpResult() != null) {
            builder.setPvpResult(buildPvpResult(data.getPvpResult()));
        }
        return builder;
    }

    /**
     * build 项目执行汇总结果
     * @param data
     * @return
     */
    public static ApprenticeProto.ApprenticeProgramResult.Builder buildProgramResult(ApprenticeProgramResult data) {
        ApprenticeProto.ApprenticeProgramResult.Builder builder = ApprenticeProto.ApprenticeProgramResult.newBuilder();
        for (ApprenticeTalentData talentData : data.getTalentDataMap().values()) {
            builder.addAddTalent(buildTalentData(talentData));
        }
        for (ApprenticeEffectBuffProject item : data.getSpecialProjectIds()) {
            builder.addSpecialProjectIds(buildEffectBuffProject(item));
        }
        builder.setReward(data.getReward());
        return builder;
    }

    /**
     * build 生效的buff项目
     * @param buffProject
     * @return
     */
    public static ApprenticeProto.ApprenticeEffectBuffProject.Builder buildEffectBuffProject(ApprenticeEffectBuffProject buffProject) {
        ApprenticeProto.ApprenticeEffectBuffProject.Builder builder = ApprenticeProto.ApprenticeEffectBuffProject.newBuilder();
        builder.setProjectId(buffProject.getProjectId());
        builder.setAddTalentRate(buffProject.getAddTalentRate());
        return builder;
    }

    /**
     * build pvp日志
     * @param log
     * @return
     */
    public static ApprenticeProto.ApprenticePvpLog.Builder buildPvpLog(ApprenticeUserPvpLog log) {
        ApprenticeProto.ApprenticePvpLog.Builder builder = ApprenticeProto.ApprenticePvpLog.newBuilder();
        builder.setUserId(log.getUserId());
        builder.setTargetUserId(log.getTargetUserId());
        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(log.getUserId());
        if (userBaseInfo != null) {
            builder.setUserBase(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        }
        UserBaseInfo targetUserBaseInfo = CrossUserMgr.getUserBaseInfo(log.getTargetUserId());
        if (targetUserBaseInfo != null) {
            builder.setTargetUserBase(PlayerBasePb.parsePlayerBaseTempMsg(targetUserBaseInfo));
        }
        builder.setWinFlag(log.isWinFlag());
        builder.setAddTalent(buildTalentData(log.getAddTalent()));
        for (ApprenticeTalentData talentData : log.getTalents()) {
            builder.addTalents(buildTalentData(talentData));
        }
        for (ApprenticeTalentData talentData : log.getTargetTalents()) {
            builder.addTargetTalents(buildTalentData(talentData));
        }
        builder.setTime(log.getTime());
        return builder;
    }

    /**
     * build 宴会日志
     * @param log
     * @return
     */
    public static ApprenticeProto.ApprenticeBanquetLog.Builder buildBanquetLog(ApprenticeUserBanquetLog log) {
        ApprenticeProto.ApprenticeBanquetLog.Builder builder = ApprenticeProto.ApprenticeBanquetLog.newBuilder();
        builder.setUserId(log.getUserId());
        builder.setTargetUserId(log.getTargetUserId());
        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(log.getUserId());
        if (userBaseInfo != null) {
            builder.setUserBase(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        }
        UserBaseInfo targetUserBaseInfo = CrossUserMgr.getUserBaseInfo(log.getTargetUserId());
        if (targetUserBaseInfo != null) {
            builder.setTargetUserBase(PlayerBasePb.parsePlayerBaseTempMsg(targetUserBaseInfo));
        }
        builder.setAddTalent(buildTalentData(log.getAddTalent()));
        builder.setTargetAddTalent(buildTalentData(log.getTargetAddTalent()));
        builder.setTime(log.getTime());
        return builder;
    }
}
